package com.yanqu.road.dao.impl.activity.seacraft;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCrossRecordData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/14 14:22
 */
public class SeacraftSouthSeaCrossRecordDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaCrossRecordData> {

    public static String getSeacraftSouthSeaCrossRecordDataSql = "select * from `t_c_activity_seacraft_southsea_record` where `activityId`=? and `time`>?  ";
    public List<SeacraftSouthSeaCrossRecordData> getSeacraftSouthSeaCrossRecordDataList(int activityId,long time) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SeacraftSouthSeaCrossRecordData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaCrossRecordDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaCrossRecordData item = parse(rs);
                list.add(item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateSeacraftSouthSeaCrossRecordDataSql = "update `t_c_activity_seacraft_southsea_record` set sonType=?,userId=?,targetId=?,killNum=?,score=?,durable=?,goodsUseNum=?,time=?,propId=?,speed=?,continueTime=? where activityId=? and unionId=? and masterType=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaCrossRecordData seacraftSouthSeaCrossRecordData) {
        seacraftSouthSeaCrossRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getSonType()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getGoodsUseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossRecordData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getPropId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getContinueTime()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossRecordData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getMasterType()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaCrossRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaCrossRecordDataSql = "insert into `t_c_activity_seacraft_southsea_record`(activityId,unionId,masterType,sonType,userId,targetId,killNum,score,durable,goodsUseNum,time,propId,speed,continueTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaCrossRecordData seacraftSouthSeaCrossRecordData) {
        seacraftSouthSeaCrossRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossRecordData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getMasterType()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getSonType()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossRecordData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossRecordData.getTargetId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getDurable()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getGoodsUseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossRecordData.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getPropId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getSpeed()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossRecordData.getContinueTime()));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaCrossRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaCrossRecordData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaCrossRecordData seacraftSouthSeaCrossRecordData = new SeacraftSouthSeaCrossRecordData();
        seacraftSouthSeaCrossRecordData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaCrossRecordData.setUnionId(rs.getString("unionId"));
        seacraftSouthSeaCrossRecordData.setMasterType(rs.getInt("masterType"));
        seacraftSouthSeaCrossRecordData.setSonType(rs.getInt("sonType"));
        seacraftSouthSeaCrossRecordData.setUserId(rs.getLong("userId"));
        seacraftSouthSeaCrossRecordData.setTargetId(rs.getString("targetId"));
        seacraftSouthSeaCrossRecordData.setKillNum(rs.getInt("killNum"));
        seacraftSouthSeaCrossRecordData.setScore(rs.getInt("score"));
        seacraftSouthSeaCrossRecordData.setDurable(rs.getInt("durable"));
        seacraftSouthSeaCrossRecordData.setGoodsUseNum(rs.getInt("goodsUseNum"));
        seacraftSouthSeaCrossRecordData.setTime(rs.getLong("time"));
        seacraftSouthSeaCrossRecordData.setPropId(rs.getInt("propId"));
        seacraftSouthSeaCrossRecordData.setSpeed(rs.getInt("speed"));
        seacraftSouthSeaCrossRecordData.setContinueTime(rs.getInt("continueTime"));

        return seacraftSouthSeaCrossRecordData;
    }


}
